package com.brillilab.service.core.lab.impl;

import java.util.List;

import com.brillilab.common.constant.KeyProfixConstant;
import com.brillilab.common.redis.RedisCache;
import com.brillilab.domain.po.lab.Lab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.lab.LabDeviceMapper;
import com.brillilab.domain.enums.common.IsDeleteEnum;
import com.brillilab.domain.po.lab.LabDevice;
import com.brillilab.service.core.lab.ILabDeviceService;

import javax.annotation.Resource;

@Service
@Transactional
public class LabDeviceServiceImpl implements ILabDeviceService {

	@Resource
	private LabDeviceMapper labDeviceMapper;
	@Resource
	private RedisCache redisCache;

	@Override
	public LabDevice add(Long labId,String deviceNo) {
		LabDevice labDevice = this.selectByNo(deviceNo);
		Assert.isTrue(labDevice==null , "设备已绑定其他实验室");
		
		labDevice = new LabDevice();
		labDevice.setLabId(labId);
		labDevice.setDeviceNo(deviceNo);
		labDevice.setState(0);
		labDevice.setIsDelete(IsDeleteEnum.NO.getValue());
		List<LabDevice> list = this.selectList(labId,null);
		if(list!=null) {
			labDevice.setDeviceName("一体机-"+(list.size()+1));
		}else {
			labDevice.setDeviceName("一体机-1");
		}
		int insert = labDeviceMapper.insert(labDevice);
		Assert.isTrue(insert==1 , "添加失败");		
		return labDevice;
	}

	@Override
	public boolean updateById(LabDevice labDevice) {
		int b = labDeviceMapper.updateById(labDevice);
		return b==1?true:false;
	}

	@Override
	public LabDevice selectById(Long id) {
		return labDeviceMapper.selectById(id);
	}
	
	@Override
	public LabDevice selectByNo(String deviceNo) {
		LambdaQueryWrapper<LabDevice> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(LabDevice::getDeviceNo, deviceNo);
		wrapper.eq(LabDevice::getIsDelete, IsDeleteEnum.NO.getValue());
		return labDeviceMapper.selectOne(wrapper);
	}

	@Override
	public boolean deleteById(Long id) {
		LabDevice labDevice = new LabDevice();
		labDevice.setId(id);
		labDevice.setIsDelete(IsDeleteEnum.YES.getValue());
		Integer b = labDeviceMapper.updateById(labDevice);
		return b==1 ? true: false;
	}
	
	@Override
	public List<LabDevice> selectList(Long labId,Integer state) {
		LambdaQueryWrapper<LabDevice> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(LabDevice::getLabId, labId);
		if(state!=null)
			wrapper.eq(LabDevice::getState, state);
		wrapper.eq(LabDevice::getIsDelete, IsDeleteEnum.NO.getValue());
		return labDeviceMapper.selectList(wrapper);
	}
	
	@Override
	public LabDevice bindDevice(Long labId,String deviceNo,Long deviceId) {
		LabDevice labDevice = this.selectByNo(deviceNo); 
		Assert.isTrue(labDevice!=null, "一体机设备不存在");
		Assert.isTrue(labDevice.getLabId().equals(labId), "一体机设备不属于当前实验室");
		Assert.isTrue(labDevice.getState()==0, "一体机设备非未绑定状态");
		labDevice.setDeviceId(deviceId);
		labDevice.setState(1);
		Integer b = labDeviceMapper.updateById(labDevice);
		Assert.isTrue(b==1, "一体机设备绑定失败");
		return labDevice;
	}

	@Override
	public boolean updateDeviceName(Long deviceId, String deviceName) {
		LabDevice labDevice = new LabDevice();
		labDevice.setId(deviceId);
		labDevice.setDeviceName(deviceName);
		return this.updateById(labDevice);
	}

	@Override
	public LabDevice getLabDeviceCache(String token) {
		Object obj=redisCache.getObj(KeyProfixConstant.TOKEN+token);
		if(obj instanceof LabDevice){
			return (LabDevice) obj;
		}
		return null;
	}


}
